package com.itheima.health.controller;

import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Order;
import com.itheima.health.service.OrderService;
import com.itheima.health.utils.resources.MessageConstant;
import com.itheima.health.utils.resources.RedisMessageConstant;
import com.itheima.health.utils.validateCodeUtils.ValidateCodeUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;


import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {

    @Autowired
    private RedisTemplate redisTemplate;

    @Reference
    private OrderService orderService;

    @Autowired
    private RabbitTemplate rabbitTemplate;


    //移动端发送验证码
    @GetMapping("order/sendCodeToMobile")
    public Result sendCodeToMobile(@RequestParam("telephone") String telephone) {
        try {
            //向用户发送验证码短信
            String cod = ValidateCodeUtils.generateValidateCode(4);
            System.out.println(cod + "验证码");
            //保存验证码
            redisTemplate.opsForValue().set(RedisMessageConstant.SENDTYPE_ORDER + telephone, cod, 90, TimeUnit.SECONDS);
            //使用阿里云短信服务
            // SendSms.registerSendCode(telephone,cod);
            Map map = new HashMap();
            map.put("telephone",telephone);
            map.put("cod",cod);
            //把信息存到 rabbit 上 存放的是map(可以存放各种类型)

            rabbitTemplate.convertAndSend("/czjk",map);


            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }


    @PutMapping("order/add")
    public Result add(@RequestBody Map map) {

        Order order;

        try {
            //拿到用户输入的验证码  与后台验证码作比较
            String userCod = (String) map.get("validateCode");  //表单验证码
            //拿到本地验证码还需要用户手机号
            String telephone1 = (String) map.get("telephone");
            //本地验证码
            String redisCod = (String) redisTemplate.opsForValue().get(RedisMessageConstant.SENDTYPE_ORDER + telephone1);

            //判断本地验证码是否存在
            if (StringUtils.isEmpty(redisCod)) {
                return new Result(false, MessageConstant.SEND_VALIDATECODE_TIMEOUT);
            } else {
                //比对验证码是否正确
                if (redisCod.equals(userCod)) {
                    //相同  处理核心业务
                    //需要判断预约日期  是否预约满  是否重复预约  添加订单  用户是否会员
                    System.out.println("执行到了核心业务");
                    order = orderService.add(map);

                } else {
                    //不相同  验证码错误
                    return new Result(false, MessageConstant.VALIDATECODE_ERROR);
                }
            }


        } catch (Exception e) {
            System.out.println(e.getMessage());
            return new Result(false, e.getMessage());
        }

        System.out.println(order + "返回数据");
        return new Result(true, MessageConstant.ADD_ORDER_SUCCESS, order);
    }

    //预约成功信息
    @GetMapping("order/findfindOrderById")
    public Result findfindOrderById(@RequestParam("id") String id) {
        try {
            Map map = orderService.findfindOrderById(id);
            System.out.println(map);
            return new Result(true, MessageConstant.QUERY_ORDERSETTING_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDERSETTING_FAIL);
        }
    }


}
